<!-- 权限管理 -->
<template>
  <div class="module_">
    <div class="flex-space-between mb8 search_box">
      <!-- <el-form :inline="true" :model="formInline" class="user-search">
        <el-form-item label="搜索：">
          <el-input
            size="small"
            v-model="formInline.machineNo"
            placeholder="输入职员姓名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            v-model="formInline.orderNo"
            placeholder="输入订单号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            v-model="formInline.transId"
            placeholder="输入交易单号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            @click="search"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="search">重置</el-button>
        </el-form-item>
      </el-form> -->
      <p></p>
      <el-button
        class="mr8"
        size="small"
        type="primary"
        @click="handle(null, {}, 3)"
        >添加权限</el-button
      >
    </div>
    <el-table
      :data="tableData"
      class="flex-1"
      height="150"
      size="small"
      border
      highlight-current-row
      v-loading="tableLoading"
      @sort-change="tableSortChange"
      stripe
    >
      <el-table-column
        align="center"
        prop="name"
        label="权限名称"
        width="180"
        sortable="custom"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="display_name"
        label="权限显示名称"
        width="180"
        sortable="custom"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="description"
        label="描述"
        min-width="600"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handle(scope.$index, scope.row, 2)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteUser(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>

    <el-dialog
      class="dialog"
      :visible.sync="editFormVisible"
      width="50%"
      @click="editFormVisible = false"
      :close-on-click-modal='false'
      :close-on-press-escape='false'
    >
      <el-form label-width="120px" :model="editForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="权限名称" class="tip">
              <el-input
                size="small"
                v-model="editForm.name"
                auto-complete="off"
                placeholder="请输入权限名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="显示名称" class="tip">
              <el-input
                size="small"
                v-model="editForm.display_name"
                auto-complete="off"
                placeholder="请输入权限显示名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="描述" class="tip">
              <el-input
                type="textarea"
                v-model="editForm.description"
                placeholder="请输入权限描述"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit" :loading='btnLoading'>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  Apipermissions,
  ApipermissionsAdd,
  ApipermissionsPut,
  ApipermissionsDel,
} from "@/api";
import { debounce } from "@/utils";
export default {
  data() {
    return {
      tableData: [],
      editFormVisible: false,
      editForm: {}, //修改
      pageparm: {
        page: 1,
        perPage: 20,
        total: null,
        sort_str: "updated_at",
        sort_order: "desc",
      },
      formInline: {},
      tableLoading: false,
      btnLoading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    tableSortChange(column) {
      if (column.order === "descending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "desc";
      } else if (column.order === "ascending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "asc";
      }else{
        this.pageparm.sort_str = null;
        this.pageparm.sort_order = null;
      }
      this.getList();
    },
    //搜索
    search() {},
    //分页
    callFather(pageparm) {
      this.pageparm.page = pageparm.page;
      this.pageparm.perPage = pageparm.perPage;
      this.getList();
    },
    getList() {
      this.tableLoading = true;
      Apipermissions(this.pageparm)
        .then(({ data, page }) => {
          this.tableData = data || [];
          this.pageparm.total = page.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    handle(index, value, i) {
      this.editForm = { ...value };
      this.editFormVisible = true;
    },
    deleteUser(index, value) {
      this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '加载中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          ApipermissionsDel(value.id).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }).finally(()=>{
            loading.close()
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submit: debounce(function () {
      //提交  修改或新增
      const editForm = this.editForm;
      if (!editForm.display_name || !editForm.description || !editForm.name) {
        this.$message.error("请输入全部信息");
        return;
      }
      this.btnLoading = true
      let options = {
        name: editForm.name,
        display_name: editForm.display_name,
        description: editForm.description, //editForm.department.id
      };
      if (editForm.id) {
        //有ID就是修改
        ApipermissionsPut(options, editForm.id)
          .then((res) => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            this.editFormVisible = false;
            this.getList();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "修改失败!",
            });
          }).finally(()=>{
            this.btnLoading = false
          });
      } else {
        ApipermissionsAdd(options)
          .then((res) => {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            this.editFormVisible = false;
            this.getList();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "添加失败!",
            });
          }).finally(()=>{
            this.btnLoading = false
          });
      }
    },500,true),
  },
};
</script>

<style lang='less' scoped>
.dialog {
  .el-col-12 {
    width: 70%;
  }
}
</style>